package com.hdoit.ares.logical.tp.zzb.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.tp.zzb.dao.IZzbDataCallbackDao;
import com.hdoit.ares.logical.tp.zzb.model.ZzbDataCallback;
import com.hdoit.ares.logical.tp.zzb.service.IZzbDataCallbackService;
import com.hdoit.ares.logical.viewmodel.tp.zzp.ZzbDataCallbackSearch;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.xiaoleilu.hutool.json.JSONUtil;
/**
 * @author      chenghuan
 * @email       
 * @createtime  2017年10月26日 15:10:42
 * @comment
 */
@Component
public class ZzbDataCallbackServiceImpl extends SuperService<ZzbDataCallback> implements IZzbDataCallbackService{
	
	private IZzbDataCallbackDao zzbDataCallbackDao;
	
	public IZzbDataCallbackDao getZzbDataCallbackDao() {
		return zzbDataCallbackDao;
	}
	@Resource
	public void setZzbDataCallbackDao(IZzbDataCallbackDao zzbDataCallbackDao) {
		this.zzbDataCallbackDao = zzbDataCallbackDao;
	}

	@Override
	public List<ZzbDataCallback> queryList(ZzbDataCallbackSearch zzbDataCallbackSearch, PagerModel pagerModel) {
		return zzbDataCallbackDao.queryList(zzbDataCallbackSearch, pagerModel);
	}
	
	// 车险通用解析
	@Override
	public ZzbDataCallback resolveZzbCallback(String requestBody) {
		if (StringUtils.isEmpty(requestBody)) {
			return null;
		}
		ZzbDataCallback zzbDataCallback = new ZzbDataCallback();
		Map<String, Object> dataCallbackMap = JSONUtil.parseObj(requestBody);

		zzbDataCallback.setId(CommonUtils.getKeyId());
		zzbDataCallback.setRequestDate(DateUtils.getCurrent());
		zzbDataCallback.setModelStatus(ModelStatus.add);

		zzbDataCallback.setTaskId(MapUtils.getString(dataCallbackMap, "taskId"));
		zzbDataCallback.setPrvId(MapUtils.getString(dataCallbackMap, "prvId"));
		zzbDataCallback.setPrvName(MapUtils.getString(dataCallbackMap, "prvName"));
		zzbDataCallback.setRespCode(MapUtils.getString(dataCallbackMap, "respCode"));
		zzbDataCallback.setErrorMsg(MapUtils.getString(dataCallbackMap, "errorMsg"));
		zzbDataCallback.setChannelId(MapUtils.getString(dataCallbackMap, "channelId"));
		zzbDataCallback.setChannelUserId(MapUtils.getString(dataCallbackMap, "channelUserId"));
		zzbDataCallback.setTaskState(MapUtils.getString(dataCallbackMap, "taskState"));
		zzbDataCallback.setTaskStateDescription(MapUtils.getString(dataCallbackMap, "taskStateDescription"));
		zzbDataCallback.setCarInfo(MapUtils.getString(dataCallbackMap, "carInfo"));
		zzbDataCallback.setCarOwner(MapUtils.getString(dataCallbackMap, "carOwner"));
		zzbDataCallback.setApplicant(MapUtils.getString(dataCallbackMap, "applicant"));
		zzbDataCallback.setInsured(MapUtils.getString(dataCallbackMap, "insured"));
		zzbDataCallback.setInsureInfo(MapUtils.getString(dataCallbackMap, "insureInfo"));
		zzbDataCallback.setMsgType(MapUtils.getString(dataCallbackMap, "msgType"));
		zzbDataCallback.setImageInfos(MapUtils.getString(dataCallbackMap, "imageInfos"));
		zzbDataCallback.setDelivery(MapUtils.getString(dataCallbackMap, "delivery"));
		try {
			String quoteValidTime = MapUtils.getString(dataCallbackMap, "quoteValidTime");
			if (!StringUtils.isEmpty(quoteValidTime)) {
				zzbDataCallback.setQuoteValidTime(DateUtils.parseDate(quoteValidTime));
			}
			String payValidTime = MapUtils.getString(dataCallbackMap, "payValidTime");
			if (!StringUtils.isEmpty(payValidTime)) {
				zzbDataCallback.setPayValidTime(DateUtils.parseDate(payValidTime));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		zzbDataCallback.setInsureSupplys(MapUtils.getString(dataCallbackMap, "insureSupplys"));
		
		zzbDataCallback.setScoreRate(MapUtils.getString(dataCallbackMap, "scoreRate"));
		
		return zzbDataCallback;
	}

}